package FirstStage.DuoTai;


public class DuoTaiShuZu {
    public static void main(String[] args) {
        // 多态数组： 数组的定义类型为父类类型，里面保存的实际元素类型为子类类型
        Person[] persons = new Person[5];
        persons[0] = new Person("mick",20);
        persons[1] = new Student("jack",20,100);
        persons[2] = new Student("smith",21,30.1);
        persons[3] = new Teacher("scott",30,10000);
        persons[4] = new Teacher("king",40,20000);

        for (int i = 0; i < persons.length; i++) {
            // person[i] 的编译类型是Person，运行类型是根据实际情况由JVM机来判断
            System.out.println(persons[i].say()); // 动态绑定机制
        }

        // 调用子类特有的方法
        for (int i = 0; i < persons.length; i++) {
            if (persons[i] instanceof Student){
               Student student =(Student)persons[i];
               student.study();
            } else if (persons[i] instanceof  Teacher) {
                Teacher teacher =(Teacher) persons[i];
                teacher.teach();
            }else {
            }
        }
    }
}
